import Vue from 'vue'
export function treeToArray (
  data,
  expandAll,
  parent = null,
  level = null
) {
  let tmp = []
  Array.from(data).forEach(function (record) {
    if (record._expanded === undefined) {
      Vue.set(record, '_expanded', expandAll)
    }
    let _level = 1
    if (level !== undefined && level !== null) {
      _level = level + 1
    }
    Vue.set(record, '_level', _level)
    // 如果有父元素
    if (parent) {
      Vue.set(record, 'parent', parent)
    }
    tmp.push(record)
    if (record.children && record.children.length > 0) {
      const children = treeToArray(record.children, expandAll, record, _level)
      tmp.children = children
    }
  })
  return tmp
}

export function getTreeNode (value, data, prop) {
  Array.from(data).forEach((record) => {
    if (record[prop] === value) {
      return record
    }
    if (data.children && data.children.length > 0) {
      let d = getTreeNode(value, data.children, prop)
      if (d !== null && d[prop] === value) {
        return d
      }
    }
  })
}